/**
 * Created with IntelliJ IDEA.
 * Description:排序
 * User: LXW
 * Date: 2022-03-04
 * Time: 15:16
 */
public class Test {
    public int[] array;
    public void mergeSort(int[] array,int[] temp,int left,int right) {
        if(left >= right) {
            return;
        }
        int center = left+(right-left)/2;
        mergeSort(array,temp,left,center);
        mergeSort(array,temp,center+1,right);
        merge(array,temp,left,center,right);
    }
    public void merge(int[] array,int[] temp,int left,int center,int right) {
        int s1 = left;
        int e1 = center;
        int s2 = center+1;
        int e2 = right;
        int length = right-left;
        int[] extra = new int[length];
        int k = 0;
        while (s1 < e1 && s2 < e2) {
            if (array[s1] <= array[s2]) {
                extra[k++] = array[s1++];
            }else {
                extra[k++] = array[s2++];
            }
        }
        while (s1 < e1) {
            extra[k++] = array[s1++];
        }
        while (s2 < e2) {
            extra[k++] = array[s2++];
        }
        for (int t = 0; t < length; t++) {
            array[s1+t] = extra[t];
        }
    }
    public void quickSort(int[] array,int left,int right) {
        if (left >= right) {
            return;
        }
        int pivot = partiton(array,left,right);
        quickSort(array,left,pivot-1);
        quickSort(array,pivot+1,right);
    }
    public int partiton(int[] array,int left,int right) {
        int tmp = array[left];
        while(left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        return left;
    }
    /**
     * 堆排序
     * 时间复杂度：n*log(n)
     * @param array
     */
    public void heapSort(int[] array) {
        createHeap(array);
        int end = array.length-1;
        while(end > 0) {
            swap(array,0,end);
            end--;
        }
    }
    /**
     * 向下调整
     * @param parent
     * @param size
     */
    public void shiftDown(int parent,int size) {
        int child = 2*parent-1;
        while (child < size) {
            if (child+1 < size && array[child] < array[child+1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                child = parent;
                parent = (child-1)/2;
            }else {
                break;
            }
        }
    }
    public void createHeap(int[] array) {
        int size = array.length;
        for (int parent = (size-1-1)/2; parent > 0 ; parent--) {
            shiftDown(parent,size);
        }
    }
    /**
     * 希尔排序
     * 时间复杂度：与增量有关
     * 是直接排序的优化
     * @param array
     */
    public void shellSort(int[] array) {
        int n = array.length;
        for (int gap = n/2; gap > 0 ; gap/= 2) {
            insert(array,gap);
        }
    }
    public void insert(int[] array,int gap) {

        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j-=gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    /**
     * 插入排序
     * 时间复杂度：O(n^2)
     * 数据越有序越高效
     * @param array
     */
    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j] = tmp;
        }
    }
    /**
     * 选择排序
     * 时间复杂度：O(n^2)
     * @param array
     */
    public void selectSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            int min = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[min]) {
                    min = j;
                }
            }
            swap(array,i,min);
        }
    }
    /**
     * 冒泡排序
     * 时间复杂度O(n^2)
     * @param array
     */
    public void bubbleSort(int[] array) {
        if(array == null || array.length <= 1) {
            return;
        }
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if (flag == false) {
                break;
            }
        }
    }
    public void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void main(String[] args) {

    }
}
